package com.cisc.controller;

import com.cisc.business.order.dto.OrderInfo;
import com.cisc.business.order.service.OrderService;
import com.cisc.business.order.service.ProductService;
import com.cisc.business.order.service.UserService;
import com.cisc.business.product.dto.ProductInfo;
import com.cisc.business.user.dto.UserInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: XXX
 * @author: daizh
 * @time: 2021/6/20 14:23
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private UserService userService;

    @Resource
    private ProductService productService;

    @GetMapping("/orderId/{id}")
    public OrderInfo getOrderInfoById(@PathVariable("id") Integer id) {
        return orderService.getOrderInfoById(id);
    }

    @GetMapping("/userId/{userId}")
    public List<OrderInfo> getOrderInfoByUserId(@PathVariable("userId") Integer userId) {
        return orderService.getOrderInfoByUserId(userId);
    }

    @GetMapping("/productId/{productId}")
    public List<OrderInfo> getOrderInfoByProductId(@PathVariable("productId") Integer productId) {
        return orderService.getOrderInfoByProductId(productId);
    }

    @GetMapping("/orders")
    public List<OrderInfo> getAllOrders() {
        return orderService.getAllOrders();
    }

    @PostMapping("/saveOrder")
    public String saveOrder(@RequestBody OrderInfo orderInfo) {
        try {
            orderService.saveOrder(orderInfo);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }


    @PostMapping("/createOrder")
    public String createOrder(Integer userId, Integer productId) {
        try {
            orderService.createOrder(userId, productId);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }

    @GetMapping("/userList")
    public List<UserInfo> getUserList(){
        return userService.getAllUsers();
    }

    @GetMapping("/productList")
    public List<ProductInfo> getProductList() {
        return productService.getAllProducts();
    }
}
